<?php

namespace app\services;

use plugin\admin\app\model\Coupon;
use plugin\admin\app\model\Notice;
use plugin\admin\app\model\User;
use plugin\admin\app\model\UserCoupon;
use plugin\admin\app\model\UserLevel;

class VIPService
{
    public static function config()
    {
        $config = ConfigService::get('site');

        return $config;
    }

    public static function toVip($userId, $level)
    {
        $userInfo = User::find($userId);

        $userInfo->level = $level;

        if ($userInfo['is_vip'] && $level == $userInfo['level']) {
            $userInfo['expiry_time'] = strtotime('+1 year', $userInfo['expiry_time']);
            $userInfo['download_count'] = $userInfo['download_count'] + UserLevel::where('id', $level)->value('download_count');
        } else {
            $userInfo['is_vip'] = 1;
            $userInfo['expiry_time'] = strtotime('+1 year');
            $userInfo['download_count'] = UserLevel::where('id', $level)->value('download_count');
        }

        $levelInfo = UserLevel::where('id', $level)->first();

        Notice::add($userId, '增值服务', sprintf("您已成功开通%s，有效期至%s", $levelInfo['name'], date('Y-m-d H:i:s', $userInfo['expiry_time'])), Notice::TYPE_PAYMENT_VIP);

        $userInfo->save();

    }
}